package com.jtny.nytb.config.dataalarm.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.contants.GenericConstants;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.core.util.QueryWrapperUtils;
import com.jtny.component.dto.CurrentUserDTO;
import com.jtny.component.entity.LocalHolder;
import com.jtny.component.entity.PageData;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.CurrentUserUtils;
import com.jtny.nytb.config.dataalarm.dao.DataAlarmDAO;
import com.jtny.nytb.config.dataalarm.domain.DataAlarmDO;
import com.jtny.nytb.config.dataalarm.dto.DataAlarmDTO;
import com.jtny.nytb.config.dataalarm.dto.DataAlarmPageQueryParamDTO;
import com.jtny.nytb.config.dataalarm.enums.DataAlarmSourceEnum;
import com.jtny.nytb.config.dataalarm.errorcode.DataAlarmErrorCodeEnum;
import com.jtny.nytb.config.dataalarm.service.DataAlarmBiz;
import com.jtny.nytb.config.dataalarm.vo.DataAlarmExcelVO;
import com.jtny.nytb.config.dataalarm.vo.DataAlarmPageQueryParamVO;
import com.jtny.nytb.config.dataalarm.vo.DataAlarmVO;
import com.jtny.nytb.config.dictionary.dao.TssoConfigDataDictDao;
import com.jtny.nytb.config.dictionary.dao.TssoConfigDataDictionaryDao;
import com.jtny.nytb.config.dictionary.domain.DictionaryDO;
import com.jtny.nytb.config.dictionary.domain.DictionaryDataDO;
import com.jtny.nytb.config.dictionary.dto.DictionaryDTO;
import com.jtny.nytb.config.dictionary.dto.DictionaryDataDTO;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据报警点管理Biz实现类
 * @author 智慧能源中心 - zx
 * @package com.jtny.nytb.config.generic.dataalarm.biz.impl
 * @fileName EquipmentInfoController.java
 * @createTime 2023年10月16日 10:47
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
public class DataAlarmBizImpl implements DataAlarmBiz {

    @Autowired
    private DataAlarmDAO dataAlarmDAO;

    @Autowired
    private TssoConfigDataDictDao dictionaryDAO;

    @Autowired
    private TssoConfigDataDictionaryDao dictionaryDataDAO;

    //todo 数据参量
    @Autowired
//    private ConfigDataParamManagementClient configDataParamManagementClient;
    //todo
//    @Autowired
//    private ConfigDataParamAlarmRelationDAO configDataParamAlarmRelationDAO;
    private static final String ALARM_TEMPLATE_NAME = "页面映射模板";


    /**
     * 保存数据报警点管理
     * @param dataAlarmVO
     * @return
     * @throws ServiceException
     */
    @Override
    public DataAlarmVO saveDataAlarm(DataAlarmVO dataAlarmVO) throws ServiceException {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();

        //封装DTO对象
        DataAlarmDTO dataAlarmDTO = BeanUtils.copyToObject(dataAlarmVO, DataAlarmDTO.class);
        //查询所有的报警点信息
        //数据库操作
        List<DataAlarmDTO> dataAlarmDTOList = getDataAlarm();
        //查询所有的报警点名称
        List<String> collectDataAlarm = dataAlarmDTOList.stream().map(DataAlarmDTO::getAlarmName).collect(Collectors.toList());
        collectDataAlarm.forEach(obj -> {
            if (obj.equals(dataAlarmVO.getAlarmName())) {
                throw new ServiceException(DataAlarmErrorCodeEnum.ALARM_NAME.getErrorCode(),DataAlarmErrorCodeEnum.ALARM_NAME.getErrorMessage());
            }
        });
        CurrentUserUtils.setCreateDefaultField(dataAlarmDTO,currentUserDTO);
        //保存
        //数据库操作
        DataAlarmDTO dataAlarmDTOFirst = save(dataAlarmDTO);

        return BeanUtils.copyToObject(dataAlarmDTOFirst,DataAlarmVO.class);
    }

    /**
     * 数据报警点修改操作
     * @param dataAlarmVO
     * @return
     * @throws ServiceException
     */
    @Override
    public DataAlarmVO updateDataAlarm(DataAlarmVO dataAlarmVO) throws ServiceException {
        Long dataAlarmId = dataAlarmVO.getId();
        if (dataAlarmId == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.PARAM_DATA_ALARM_ID_IS_NULL.getErrorCode(),DataAlarmErrorCodeEnum.PARAM_DATA_ALARM_ID_IS_NULL.getErrorMessage());
        }

        //通过报警点id获取数据报警DTO对象
        //数据库操作
        DataAlarmDTO dataAlarmDTO = getById(dataAlarmId);
        if (dataAlarmDTO == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(),DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }
        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的数据报警DTO对象
        BeanUtils.copyObject(dataAlarmVO, dataAlarmDTO);
        CurrentUserUtils.setUpdateDefaultField(dataAlarmDTO, currentUserDTO);
        //修改操作
        //数据库操作
        update(dataAlarmDTO);
        return dataAlarmVO;
    }

    /**
     * 数据报警点管理删除操作
     * @param id
     * @throws ServiceException
     */
    @Override
    public void deleteDataAlarm(Long id) throws ServiceException {
        //通过报警点id获取报警点DTO对象
        //数据库操作
        DataAlarmDTO dataAlarmDTO = getById(id);
        if (dataAlarmDTO == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(),DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }
        //删除操作
        //数据库操作
        delete(id);
    }

    /**
     * 通过报警点id获取报警点单个对象
     * @param id
     * @throws ServiceException
     */
    @Override
    public DataAlarmVO detailsDataAlarm(Long id) throws ServiceException {
        //通过报警点id获取报警点DTO对象
        //数据库操作
        DataAlarmDTO dataAlarmDTO = getById(id);
        if (dataAlarmDTO == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(),DataAlarmErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }
        return BeanUtils.copyToObject(dataAlarmDTO,DataAlarmVO.class);
    }

    /**
     * 数据报警点管理信息分页查询操作
     * @param dataAlarmPageQueryParamVO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<DataAlarmVO> queryPage(DataAlarmPageQueryParamVO dataAlarmPageQueryParamVO) throws ServiceException {
        //校验参数处理
        if (dataAlarmPageQueryParamVO == null) {
            dataAlarmPageQueryParamVO = new DataAlarmPageQueryParamVO();
            dataAlarmPageQueryParamVO.setPageNo(GenericConstants.DEFAULT_PAGE_NO);
            dataAlarmPageQueryParamVO.setPageSize(GenericConstants.DEFAULT_PAGE_SIZE);
        }

        //分页查询用户信息
        DataAlarmPageQueryParamDTO dataAlarmPageQueryParamDTO = BeanUtils.copyToObject(dataAlarmPageQueryParamVO, DataAlarmPageQueryParamDTO.class);
        //数据库操作
        PageData<DataAlarmDTO> pageDataForDTO = queryPage(dataAlarmPageQueryParamDTO);
        if (pageDataForDTO == null || CollUtil.isEmpty(pageDataForDTO.getData())) {
            return new PageData<>();
        }

        Collection<DataAlarmDTO> dataAlarmDTOS = pageDataForDTO.getData();
        List<DataAlarmVO> dataAlarmVOList = new ArrayList<>(dataAlarmDTOS.size());
        dataAlarmDTOS.forEach(dataAlarmDTO -> {
            DataAlarmVO dataAlarmVO = this.getDataAlarmVOByDTO(dataAlarmDTO);
            dataAlarmVOList.add(dataAlarmVO);
        });

        PageData<DataAlarmVO> pageDataForVO = pageDataForDTO.transformExcludeData(DataAlarmVO.class);
        pageDataForVO.setData(dataAlarmVOList);

        return pageDataForVO;
    }

    /**
     * 下载报警点excel模板
     * @param response
     */
    @Override
    public void downloadDataAlarmTemplate(HttpServletResponse response) {

        //文件名
        String fileName = ALARM_TEMPLATE_NAME;

        //导出报警点模板
        EasyExcelUtils.exportExcel(response, fileName, null, DataAlarmExcelVO.class);
    }

    /**
     * 导入报警点数据
     * @param file
     */
    @Override
    public void importDataAlarm(MultipartFile file) {

        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();

        List<DataAlarmVO> dataAlarmVOList = new ArrayList<>();
        //导入excel文件
        List<DataAlarmExcelVO> basicBeanList = (List<DataAlarmExcelVO>) EasyExcelUtils.importExcel(file, DataAlarmExcelVO.class);

        if (CollectionUtil.isEmpty(basicBeanList)) {
            throw new ServiceException("导入的数据为空");
        }
        basicBeanList.forEach(dataAlarmExcelVO -> {
            DataAlarmVO dataAlarmVO = new DataAlarmVO();
            BeanUtils.copyObject(dataAlarmExcelVO, dataAlarmVO);
            dataAlarmVOList.add(dataAlarmVO);
        });
        List<DictionaryDTO> dictionaryDTOS = new ArrayList<>();
        for (DataAlarmVO dataAlarmVO : dataAlarmVOList) {
            //通过字典项名称查询单个字典项对象
            //dictionary数据库操作
            DictionaryDTO dictionaryDTO = selectDictionaryByName("报警点分类");
            dictionaryDTOS.add(dictionaryDTO);
        }
        List<Long> collectDictionaryDTOId = dictionaryDTOS.stream().map(DictionaryDTO::getId).collect(Collectors.toList());
        List<String> collectAlarmType = dataAlarmVOList.stream().map(DataAlarmVO::getAlarmType).collect(Collectors.toList());
        List<DictionaryDataDTO> dictionaryDataDTOList = new ArrayList<>();
        if (collectDictionaryDTOId != null && collectDictionaryDTOId.size() > 0 && CollectionUtil.isNotEmpty(collectAlarmType)) {
            for (int i = 0; i < collectDictionaryDTOId.size() && i < collectAlarmType.size() ; i++) {
                DictionaryDataDTO dictionaryDataDTO = new DictionaryDataDTO();
                dictionaryDataDTO.setDictId(collectDictionaryDTOId.get(i));
                dictionaryDataDTO.setDescription(collectAlarmType.get(i));
                //dictionaryData数据库操作
                DictionaryDataDTO dictionaryDataDTOFirst = queryByDescriptionAndDictId(dictionaryDataDTO);
                //判断是否查出单个对象，查出则继续，查不出则抛异常
                if (dictionaryDataDTOFirst == null) {
                    throw new ServiceException("导入的对象数据当中有个别数据没有匹配到字典项的名称，请注意检查一下");
                }
                dictionaryDataDTOList.add(dictionaryDataDTOFirst);
            }
            //从字典项查出值
            List<String> collectValue = dictionaryDataDTOList.stream().map(DictionaryDataDTO::getValue).collect(Collectors.toList());
            List<DataAlarmVO> objectsDataAlarmVOs = new ArrayList<>();
//            int count1 = Math.min(Math.min(dataAlarmVOList.size(), collectValue.size()));
            for (int i = 0; i <collectValue.size() && i <dataAlarmVOList.size(); i++) {
                DataAlarmVO dataAlarmVO = dataAlarmVOList.get(i);
                dataAlarmVO.setAlarmType(collectValue.get(i));
                objectsDataAlarmVOs.add(dataAlarmVO);
            }
//            collectValue.forEach(value -> {
//                for (DataAlarmVO dataAlarmVO : dataAlarmVOList) {
//                    dataAlarmVO.setAlarmType(value);
//                    objectsDataAlarmVOs.add(dataAlarmVO);
//                }
//            });
//            // 通过参量名称判断参量表中是否存在返回的集合
            List<DataAlarmVO> dataParameter = getDataParameter(dataAlarmVOList);

            List<DataAlarmDTO> dataAlarmDTOParameterList = BeanUtils.copyToList(dataParameter, DataAlarmDTO.class);
            List<DataAlarmDTO> dataAlarmDTOTypeList = BeanUtils.copyToList(objectsDataAlarmVOs, DataAlarmDTO.class);
            List<DataAlarmDTO> alarmClassValueList = getAlarmClassValue(dataAlarmVOList);

            int count = Math.min(Math.min(dataAlarmDTOParameterList.size(), alarmClassValueList.size()), dataAlarmDTOTypeList.size());
            for (int i = 0; i < count; i++) {
                DataAlarmDTO dataAlarmDTO = dataAlarmDTOParameterList.get(i);
                DataAlarmDTO dataAlarmDTOFirst = alarmClassValueList.get(i);
                DataAlarmDTO dataAlarmDTOSecond = dataAlarmDTOTypeList.get(i);

                dataAlarmDTO.setAlarmClass(dataAlarmDTOFirst.getAlarmClass());
                dataAlarmDTO.setAlarmType(dataAlarmDTOSecond.getAlarmType());
                CurrentUserUtils.setCreateDefaultField(dataAlarmDTO, currentUserDTO);
                //数据库操作
                save(dataAlarmDTO);
            }

        }


    }

    /**
     * 处理报警等级
     * @param dataAlarmVOList
     * @return
     */
    private List<DataAlarmDTO> getAlarmClassValue(List<DataAlarmVO> dataAlarmVOList) {
        List<DictionaryDTO> dictionaryDTOS = new ArrayList<>();
        List<DataAlarmDTO> dataAlarmDTOList = new ArrayList<>();
        for (DataAlarmVO dataAlarmVO : dataAlarmVOList) {
            //通过字典项名称查询单个字典项对象
            //dictionary数据库操作
            DictionaryDTO dictionaryDTO = selectDictionaryByName("报警等级");
            dictionaryDTOS.add(dictionaryDTO);
        }
        List<Long> collectDictionaryDTOId = dictionaryDTOS.stream().map(DictionaryDTO::getId).collect(Collectors.toList());
        List<String> collectAlarmClass = dataAlarmVOList.stream().map(DataAlarmVO::getAlarmClass).collect(Collectors.toList());
        List<DictionaryDataDTO> dictionaryDataDTOList = new ArrayList<>();
        if (collectDictionaryDTOId != null && collectDictionaryDTOId.size() > 0 && CollectionUtil.isNotEmpty(collectAlarmClass)) {
            for (int i = 0; i < collectDictionaryDTOId.size() && i < collectAlarmClass.size(); i++) {
                DictionaryDataDTO dictionaryDataDTO = new DictionaryDataDTO();
                dictionaryDataDTO.setDictId(collectDictionaryDTOId.get(i));
                dictionaryDataDTO.setDescription(collectAlarmClass.get(i));
                //dictionaryData数据库操作
                DictionaryDataDTO dictionaryDataDTOFirst = queryByDescriptionAndDictId(dictionaryDataDTO);
                //判断是否查出单个对象，查出则继续，查不出则抛异常
                if (dictionaryDataDTOFirst == null) {
                    throw new ServiceException("导入的对象数据当中有个别数据没有匹配到字典项的名称，请注意检查一下");
                }
                dictionaryDataDTOList.add(dictionaryDataDTOFirst);
            }
            //从字典项查出值
            List<String> collectValue = dictionaryDataDTOList.stream().map(DictionaryDataDTO::getValue).collect(Collectors.toList());
            List<DataAlarmVO> objectsDataAlarmVOs = new ArrayList<>();
            for (int i = 0; i <collectValue.size() && i <dataAlarmVOList.size(); i++) {
                DataAlarmVO dataAlarmVO = dataAlarmVOList.get(i);
                dataAlarmVO.setAlarmClass(collectValue.get(i));
                objectsDataAlarmVOs.add(dataAlarmVO);
            }
             dataAlarmDTOList = BeanUtils.copyToList(objectsDataAlarmVOs, DataAlarmDTO.class);
        }
        return dataAlarmDTOList;
    }

    /**
     * 处理报警点参量来源
     * @param dataAlarmDTO
     */
    private void DataAlarmSource(DataAlarmDTO dataAlarmDTO) {

        //写入报警数据参量来源
        if (dataAlarmDTO.getSource().equals(DataAlarmSourceEnum.COLLECT.getDesc())) {
            dataAlarmDTO.setSource(DataAlarmSourceEnum.COLLECT.getValue());
        } else if (dataAlarmDTO.getSource().equals(DataAlarmSourceEnum.COMPUTER.getDesc())) {
            dataAlarmDTO.setSource(DataAlarmSourceEnum.COMPUTER.getValue());
        } else if (dataAlarmDTO.getSource().equals(DataAlarmSourceEnum.CONSTANT.getDesc())) {
            dataAlarmDTO.setSource(DataAlarmSourceEnum.CONSTANT.getValue());
        } else if (dataAlarmDTO.getSource().equals(DataAlarmSourceEnum.SUBMISSION.getDesc())) {
            dataAlarmDTO.setSource(DataAlarmSourceEnum.SUBMISSION.getValue());

        }
    }

    /**
     * 通过参量名称判断参量表中是否存在
     * @param dataAlarmVOList
     */
    private List<DataAlarmVO> getDataParameter(List<DataAlarmVO> dataAlarmVOList) {

        List<DataAlarmVO> objectsDataAlarmVO = new ArrayList<>();
        //通过参量名称判断参量表中是否存在
        //todo 数据参量
//        dataAlarmVOList.forEach(dataAlarmVO -> {
//            List<ConfigDataParamManagementDTO> configDataParamManagementDTOS = configDataParamManagementClient.queryByName(dataAlarmVO.getName());
//                if (configDataParamManagementDTOS.size() != 1) {
//                    throw new ServiceException("参量名称没有匹配到，请注意检查一下");
//                }
//                for (ConfigDataParamManagementDTO configDataParamManagementDTO : configDataParamManagementDTOS) {
//                    dataAlarmVO.setDataParamId(configDataParamManagementDTO.getId());
//                    dataAlarmVO.setCode(configDataParamManagementDTO.getCode());
//                    dataAlarmVO.setSource(configDataParamManagementDTO.getSource());
//                }
//                objectsDataAlarmVO.add(dataAlarmVO);
//
//
//        });
        return objectsDataAlarmVO;

    }

    private DataAlarmVO getDataAlarmVOByDTO(DataAlarmDTO dataAlarmDTO) {
        DataAlarmVO dataAlarmVO = BeanUtils.copyToObject(dataAlarmDTO, DataAlarmVO.class);
        return dataAlarmVO;
    }


    /**
     * 保存数据报警点管理
     * @param dataAlarmDTO
     * @return
     * @throws ServiceException
     */
    public DataAlarmDTO save(DataAlarmDTO dataAlarmDTO) throws ServiceException {
        if (dataAlarmDTO == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorCode(), DataAlarmErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorMessage());
        }

        // 将DTO对象的属性复制给DO对象
        DataAlarmDO dataAlarmDO = BeanUtils.copyToObject(dataAlarmDTO, DataAlarmDO.class);

        // 保存操作
        dataAlarmDAO.insert(dataAlarmDO);

        // 将id设置给DTO对象
        dataAlarmDTO.setId(dataAlarmDO.getId());

        return dataAlarmDTO;
    }

    /**
     * 查询所有的数据报警点信息
     * @return
     * @throws ServiceException
     */
    public List<DataAlarmDTO> getDataAlarm() throws ServiceException {
        List<DataAlarmDO> dataAlarmDOS = dataAlarmDAO.selectList(null);
        List<DataAlarmDTO> dataAlarmDTOList = BeanUtils.copyToList(dataAlarmDOS, DataAlarmDTO.class);
        return dataAlarmDTOList;
    }

    /**
     * 通过id查找单个数据报警点信息
     * @param id
     * @return
     * @throws ServiceException
     */
    public DataAlarmDTO getById(Long id) throws ServiceException {
        QueryWrapper<DataAlarmDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",id);
        DataAlarmDO dataAlarmDO = dataAlarmDAO.selectOne(objectQueryWrapper);
        return BeanUtils.copyToObject(dataAlarmDO,DataAlarmDTO.class);
    }

    /**
     * 修改数据报警点信息
     * @param dataAlarmDTO
     * @return
     * @throws ServiceException
     */
    public DataAlarmDTO update(DataAlarmDTO dataAlarmDTO) throws ServiceException {
        if (dataAlarmDTO == null) {
            throw new ServiceException(DataAlarmErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorCode(), DataAlarmErrorCodeEnum.PARAM_OBJECT_IS_NULL.getErrorMessage());
        }

        //报警点名称判重
        List<DataAlarmDO> dataAlarmDOS = dataAlarmDAO.selectList(Wrappers.<DataAlarmDO>lambdaQuery(DataAlarmDO.class));
        List<String> alarmNameList = dataAlarmDOS.stream().map(DataAlarmDO::getAlarmName).collect(Collectors.toList());
        alarmNameList.forEach(alarmName->{
            if (alarmName.equals(dataAlarmDTO.getAlarmName())){
                throw new ServiceException(DataAlarmErrorCodeEnum.ALARM_NAME.getErrorCode(),DataAlarmErrorCodeEnum.ALARM_NAME.getErrorMessage());
            }
        });

        // 将DTO对象的属性复制给DO对象
        DataAlarmDO dataAlarmDO = BeanUtils.copyToObject(dataAlarmDTO, DataAlarmDO.class);

        dataAlarmDAO.updateById(dataAlarmDO);
        return dataAlarmDTO;
    }

    /**
     * 删除数据报警点管理操作
     * @param id
     * @throws ServiceException
     */
    public void delete(Long id) throws ServiceException {
        dataAlarmDAO.deleteById(id);
        //todo 需要放开
//        configDataParamAlarmRelationDAO.deleteById(id);
    }

    /**
     * 分页查询数据报警点管理信息
     * @param dataAlarmPageQueryParamDTO
     * @return
     * @throws ServiceException
     */
    public PageData<DataAlarmDTO> queryPage(DataAlarmPageQueryParamDTO dataAlarmPageQueryParamDTO) throws ServiceException {
        // 封装查询条件QueryWrapper对象
        QueryWrapper<DataAlarmDO> queryWrapper = QueryWrapperUtils.getQueryWrapper(dataAlarmPageQueryParamDTO);
        queryWrapper.orderByAsc("code");
        // 分页查询数据报警点管理信息
        Page<DataAlarmDO> page = Page.of(dataAlarmPageQueryParamDTO.getPageNo(), dataAlarmPageQueryParamDTO.getPageSize());
        PageData<DataAlarmDO> pageDataForDO = PageUtils.getPageData(dataAlarmDAO.selectPage(page, queryWrapper), dataAlarmPageQueryParamDTO);

        return pageDataForDO.transformData(DataAlarmDTO.class);
    }

    /**
     * 通过报警点参量code查询报警点信息
     * @param code
     * @return
     * @throws ServiceException
     */
    public List<DataAlarmDTO> getByCode(String code) throws ServiceException {
        QueryWrapper<DataAlarmDO> dataAlarmDTOQueryWrapper = new QueryWrapper<>();
        dataAlarmDTOQueryWrapper.eq("code",code);
        List<DataAlarmDO> dataAlarmDOS = dataAlarmDAO.selectList(dataAlarmDTOQueryWrapper);
        return BeanUtils.copyToList(dataAlarmDOS,DataAlarmDTO.class);
    }

    public List<DataAlarmDTO> getByDataParamId(Long dataParamId) {
        LambdaQueryWrapper<DataAlarmDO> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(DataAlarmDO::getDataParamId,dataParamId);
        List<DataAlarmDO> dataAlarmDOS = dataAlarmDAO.selectList(objectLambdaQueryWrapper);
        return BeanUtils.copyToList(dataAlarmDOS,DataAlarmDTO.class);
    }
/**
 * 数据字典
 */
    public DictionaryDTO selectDictionaryByName(String name) {
        //封装查询条件QueryWrapper对象
        QueryWrapper<DictionaryDO> wrapper = new QueryWrapper<>();
        //设置查询条件
        wrapper.eq("name", name);
        DictionaryDO dictionaryDO = dictionaryDAO.selectOne(wrapper);
        if(dictionaryDO != null){
            return BeanUtils.copyToObject(dictionaryDO, DictionaryDTO.class);
        }else {
            return null;
        }
    }

    /**
     * 数据字典项
     * @param dictionaryDataDTO
     * @return
     */
    public DictionaryDataDTO queryByDescriptionAndDictId(DictionaryDataDTO dictionaryDataDTO) {

        QueryWrapper<DictionaryDataDO> wrapper = new QueryWrapper<>();

        if (dictionaryDataDTO != null) {

            wrapper.eq("dict_id", dictionaryDataDTO.getDictId())
                    .eq("dict_description", dictionaryDataDTO.getDescription()).eq("status", 0);

            DictionaryDataDO dictionaryDataDO = dictionaryDataDAO.selectOne(wrapper);

            return BeanUtils.copyToObject(dictionaryDataDO, DictionaryDataDTO.class);
        } else
            return null;
    }
}
